/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.client.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.text.DecimalFormat;
import javax.swing.JPanel;
import javax.swing.JTable;
import com.bullionxpress.client.application.ClientGlobals;
import com.bullionxpress.shared.listeners.AccountEvent;
import com.bullionxpress.shared.listeners.AccountListener;
import com.bullionxpress.shared.model.Instrument;
import com.bullionxpress.shared.model.Position;
import com.bullionxpress.shared.model.Quote;
import com.bullionxpress.shared.service.PositionService;
import com.bullionxpress.shared.service.AccountDatabase;
import com.bullionxpress.shared.service.AdminService;
import com.bullionxpress.shared.service.QuoteService;
import com.bullionxpress.shared.util.Log;
import javax.swing.JDialog;

/**
 *
 * @author BullionXpress.com
 */
public class ReportView extends JPanel implements AccountListener {

    private static final int CONSOLIDATED_UNITS = 0;
    private static final int CONSOLIDATED_CASH_BALANCE = 1;
    private static final int CONSOLIDATED_POSITION_VALUE = 2;
    private static final int CONSOLIDATED_VALUE = 3;

    private static final int NUMBER_OF_ACCOUNTS = 4;
    private static final int NUMBER_OF_TRANSACTIONS = 5;
    private static final int INSTRUMENT = 6;
    private static final int CURRENCY = 7;
    JTable table = null;
    GenericTableModel model = new GenericTableModel();

    JDialog owner = null;
    public ReportView(JDialog owner) {
        //this.account = account;
        this.owner = owner;
        Log.info("Adding ReportView to AccountListener");
        ClientGlobals.cm.addAccountListener(this);
        String[] columnNames = new String[]{"Key", "Value"}; // BALANCE ?
        table = new JTable(model);
        for (int i = 0; i < columnNames.length; i++) {
            model.addColumn(columnNames[i]);
        }
        table.getColumnModel().getColumn(1).setCellRenderer(new AccountCellRenderer());
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.add(table.getTableHeader(), BorderLayout.NORTH);
        p.add(table, BorderLayout.CENTER);

        p.setPreferredSize(getPreferredSize());
        p.setMaximumSize(getMaximumSize());
        p.setMinimumSize(getMinimumSize());

        setLayout(new GridLayout(1, 1));

        ///////////////////////////////////////////////////////////////////////
        Object[][] rowData = new Object[][]{
            {"Consolidated Units", ""},
            {"Consolidated Cash Balance", ""},
            {"Consolidated Position Value", ""},
            {"Total Exposure", ""},
            {"Number of Accounts", ""},
            {"Number of Transactions", ""},
            //{"New Accounts Today", ""},
            //{"Transactions Today", ""},
            {"Instrument", ""},
            {"Currency", "CAD"},
        };

        for (int i = 0; i < rowData.length; i++) {
            Object[] o = rowData[i];
            model.addRow(o);
        }
        //////////////////////////////////////////////////////////////////////
        add(p);
    }

    private final int w = 210;
    private final int h = 380;

    public Dimension getPreferredSize() {
        return new Dimension(w, h);
    }

    public Dimension getMaximumSize() {
        return new Dimension(w, h);
    }

    public Dimension getMinimumSize() {
        return new Dimension(w, h);
    }

    public void update(AccountEvent ae) {
        try {
            //Log.info("Update Report View");
            QuoteService qs = ClientGlobals.quoteService;
            DecimalFormat df = new DecimalFormat("#,###.00");
            AdminService as = ClientGlobals.adminService;
            Quote q = qs.getQuote(ClientGlobals.instrument);
            int u = as.getCondolidatedUnits(ClientGlobals.instrument);
            model.setValueAt("" + u, CONSOLIDATED_UNITS, 1);
            model.setValueAt("" + df.format(as.getCondolidatedBalance()), CONSOLIDATED_CASH_BALANCE, 1);
            model.setValueAt("" + df.format(q.getAsk() * u), CONSOLIDATED_POSITION_VALUE, 1);
            model.setValueAt("" + df.format((q.getAsk() * u) + as.getCondolidatedBalance()) , CONSOLIDATED_VALUE, 1);
            model.setValueAt("" + as.getNumberOfAccounts(), NUMBER_OF_ACCOUNTS, 1);
            model.setValueAt("" + as.getNumberOfTransactions(), NUMBER_OF_TRANSACTIONS, 1);
            model.setValueAt("" + ClientGlobals.instrument.getSymbol(), INSTRUMENT, 1);
            model.setValueAt(ClientGlobals.currency, CURRENCY, 1);

            model.fireTableDataChanged();
        } catch (Exception ex) {
        }
    }
}
